#include "tipwidget.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QStyle>
#include <QPainter>
#include<QScreen>
#include<QDebug>

TipWidget* TipWidget::m_instance = nullptr;     //定义并且初始化

TipWidget* TipWidget::getInstance()
{
    if (!m_instance)
        m_instance = new TipWidget;
    return m_instance;
}

void TipWidget::release(){
    if(m_instance != nullptr)
    {
        delete m_instance;
        m_instance = nullptr;
    }
}

TipWidget::TipWidget(QWidget *parent)
    : QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint | Qt::ToolTip); // 无边框、悬浮
    setAttribute(Qt::WA_TranslucentBackground);             // 支持透明
    setAttribute(Qt::WA_ShowWithoutActivating);


    m_iconLabel = new QLabel(this);
    m_textLabel = new QLabel(this);
    m_textLabel->setStyleSheet("color: white; "
                               "font-size: 16px; "
                               "font-weight: bold;"
                               "font-family: 'Microsoft YaHei'"
                               );
    m_textLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);

    m_timer = new QTimer(this);
    m_timer->setSingleShot(true);
    connect(m_timer, &QTimer::timeout, this, [this]() {
        // 淡出动画
        m_fadeAnimation->start();
    });

    m_fadeAnimation = new QPropertyAnimation(this, "windowOpacity");
    m_fadeAnimation->setDuration(800);          //动画时长800毫秒
    m_fadeAnimation->setStartValue(1.0);
    m_fadeAnimation->setEndValue(0.0);
    connect(m_fadeAnimation, &QPropertyAnimation::finished, this, &QWidget::hide);

    setupUI();
    m_player = new QMediaPlayer(this);
    m_player->setMedia(QUrl::fromLocalFile("F:/sound/newalert.wav"));  // 支持 mp3/wav 等
    m_player->setVolume(80); // 0 - 100
}

TipWidget::~TipWidget()
{
}


void TipWidget::setupUI()
{
    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setContentsMargins(10, 5, 10, 5);
    layout->setSpacing(5);

    layout->addWidget(m_iconLabel);
    layout->addWidget(m_textLabel, 1);

}

void TipWidget::showTip(const QString& text, TipType type, int duration, QWidget* parent)
{
    // 播放提示音
    m_player->play();
    // 设置图标
    QPixmap icon;
    switch (type) {
        case Info:
        {
            icon = QPixmap(":/img/info.png");
            break;
        }
        case Warning:
        {
            icon = QPixmap(":/img/warning.png");
            break;
        }
        case Error:
        {
            icon = QPixmap(":/img/error.png");
            break;
        }
    }
    m_iconLabel->setPixmap(icon.scaled(35, 35, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    m_textLabel->setText(text);

    adjustSize();

    // 放置在父窗口正中上方
    if (parent) {
        int x = (parent->width() - width()) / 2;
        int y = 30; // 离顶部30px
        move(parent->mapToGlobal(QPoint(x, y)));
    } else {
        // 没有父窗口则屏幕中央上方
        QScreen* screen = QGuiApplication::primaryScreen();  // 获取主屏幕
        QRect screenRect = screen->availableGeometry();      // 可用屏幕区域（不包括任务栏等）
        move((screenRect.width() - width()) / 2, 30);
    }

    setWindowOpacity(1.0);
    show();

    // 1秒后自动淡出
    m_timer->start(duration);
}

void TipWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(QColor(80, 80, 80, 220)); // 灰色带透明
    painter.setPen(Qt::NoPen);
    painter.drawRoundedRect(rect(), 10, 10);
}

